#include "CHowToPlayState.h"
#include "CMainMenuState.h"
#include "CCyberSpaceState.h"
#include "CGame.h"

#include "Game Objects\CNode.h"
#include "Game Objects\CTeleporter.h"
#include "Game Objects\CPlayer.h"
#include "CPrintBitmapToScreen.h"
#include "Wrappers\CSGD_DirectInput.h"

CHowToPlayState::CHowToPlayState(void)
{

}

CHowToPlayState::CHowToPlayState(const CHowToPlayState&)
{

}

CHowToPlayState& CHowToPlayState::operator=(const CHowToPlayState&)
{
	return *this;
}

CHowToPlayState::~CHowToPlayState(void)
{

}

CHowToPlayState* CHowToPlayState::GetInstance(void)
{
	static CHowToPlayState instance;
	return &instance;
}

void CHowToPlayState::Enter(void)
{
	m_pPlayer = CPlayer::GetInstance();
	isguide = true;

	map.Load(".\\Resource\\XML\\tut0.xml");
	map.Load(".\\Resource\\XML\\tut1.xml");
	map.Load(".\\Resource\\XML\\tut2.xml");
	map.Load(".\\Resource\\XML\\tut3.xml");

	vector<CNode*>* nodes = map.getNodes();

	(*nodes)[0]->getTeles()->operator[](0)->setLink((*nodes)[1]->getTeles()->operator[](0));
	(*nodes)[1]->getTeles()->operator[](1)->setLink((*nodes)[2]->getTeles()->operator[](0));
	(*nodes)[2]->getTeles()->operator[](1)->setLink((*nodes)[3]->getTeles()->operator[](0));

	CCyberSpaceState::GetInstance()->setmap(&map);

	// Remove all objects in object manager
	ObjectManager::GetInstance()->RemoveAllObjects();

	CCyberSpaceState::GetInstance()->Enter();
	CCyberSpaceState::GetInstance()->GetAI().SetRate(100);
}

bool CHowToPlayState::Input(void)
{
	if(ispaused == false)
	{
		if(!CCyberSpaceState::GetInstance()->Input())
		{
			return false;
		}
	}
	else
	{
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
		{
			ispaused = false;
		}
	}
	return true;
}

void CHowToPlayState::Update(float fElapsedTime)
{
	if(ispaused == false)
	{
		m_pPlayer->sethealth(100);
		CNode* prevnode = CCyberSpaceState::GetInstance()->getnode();
	
		if(prevnode != curnode)
		{
			curnode = prevnode;
			ispaused = true;
		}
		unsigned int size = map.getNodes()->size();
		for(unsigned int i = 0; i < size; i++)
		{
			if(curnode == map.getNodes()->operator[](i))
			{
				currentnode = i;
				break;
			}
		}
	
		CCyberSpaceState::GetInstance()->Update(fElapsedTime);
	}
}

void CHowToPlayState::Render(void)
{
	CPrintBitmapFontToScreen* writer =  CPrintBitmapFontToScreen::GetInstance();
	CCyberSpaceState::GetInstance()->Render();
	if(currentnode == 0)
	{
		writer->Print("Use the W,A,S,D keys to move", 30, 20, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("press tab to switch grenades", 30, 40, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("use the mouse to aim and fire your weapon", 30, 60, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("use g to throw a firewall (FW) grenade", 30, 80, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("at the firewall to your right", 30, 100, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Press space to use the teleporter", 30, 120, .7f, D3DCOLOR_XRGB(255, 0, 0));
		if(ispaused == true)
		writer->Print("press enter to continue", 150, 150, .7f, D3DCOLOR_XRGB(255, 0, 0));
	}
	if(currentnode == 1)
	{
		writer->Print("You must destroy all enemy software in order", 30, 20, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("to capture a node", 30, 40, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Destroy the turrets to the right to activate", 30, 60, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("the teleporter in order to move on", 30, 80, .7f, D3DCOLOR_XRGB(255, 0, 0));
		if(ispaused == true)
		writer->Print("press enter to continue", 150, 150, .7f, D3DCOLOR_XRGB(255, 0, 0));

	}
	if(currentnode == 2)
	{
		writer->Print("There is an AI working to lock you out of the system", 30, 20, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Nodes that you control are green on the minimap", 30, 40, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Nodes that the AI controls are red", 30, 60, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Nodes that the AI is trying to capture blink yellow", 30, 80, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("kill these worms to move on", 30, 100, .7f, D3DCOLOR_XRGB(255, 0, 0));
		if(ispaused == true)
		writer->Print("press enter to continue", 150, 150, .7f, D3DCOLOR_XRGB(255, 0, 0));
		
	}
	if(currentnode == 3)
	{
		writer->Print("The object of Hacking in cyberscape is to capture", 30, 20, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("a line of nodes from your node (far left on the", 30, 40, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("minimap) to the AI node (far right)", 30, 60, .7f, D3DCOLOR_XRGB(255, 0, 0));
		writer->Print("Kill these spiders to complete the tutorial", 30, 80, .7f, D3DCOLOR_XRGB(255, 0, 0));
		if(ispaused == true)
			writer->Print("press enter to continue", 150, 150, .7f, D3DCOLOR_XRGB(255, 0, 0));
	}
}

void CHowToPlayState::Exit(void)
{

	CCyberSpaceState::GetInstance()->Exit();
	isguide = false;
}

void CHowToPlayState::MessageProc(CBaseMessage* pMsg)
{

}